package com.example.demo.controller;

import com.example.demo.entity.Mdtype;
import com.example.demo.entity.Means;
import com.example.demo.enums.ResultEnum;
import com.example.demo.service.DataService;
import com.example.demo.vo.ResultVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/means")
public class DataController {
    @Autowired
    private DataService dataService;

    private static final Logger LOGGER = LoggerFactory.getLogger(DataController.class);

    @Value("${web.upload-path}")
    private String uploadPath;

    @RequestMapping("/findAllMeans")
    public Object findAllMeans(@RequestParam Map<String,String> map){
        Page<Object> pageInfo = PageHelper.startPage(Integer.valueOf(map.get("page")),Integer.valueOf(map.get("rows")));
        List<Means> lists = dataService.findAllMeans(map.get("dtitle"));
        return lists;
    }
    @RequestMapping("/findAllMdtype")
    public Object findAllMdtype(){
        List<Mdtype> lists  = dataService.findAllMdtype();
        return lists;
    }
    @RequestMapping("/addMdtype")
    public ResultVO addMdtype(String text){
        dataService.addMdtype(text);
        return new ResultVO(ResultEnum.SUCCESS,"添加成功");
    }
    @RequestMapping("/modifyMdtype")
    public ResultVO modifyMdtype(String id,String text){
        dataService.modifyMdtype(id,text);
        return new ResultVO(ResultEnum.SUCCESS,"修改成功");
    }
    @RequestMapping("/delMdtypeById")
    public ResultVO delMdtypeById(String id){
        dataService.delMdtypeById(id);
        return new ResultVO(ResultEnum.SUCCESS,"删除成功");
    }
    @RequestMapping("/addMeans")
    public ResultVO addMeans(@RequestPart("dannex") MultipartFile file, @RequestParam Map<String,String> map) throws IOException {
        String imgPath = "";
        if (!file.isEmpty() || file != null){
            String canonicalPath = new File(uploadPath).getCanonicalPath();
            String filename = UUID.randomUUID() + file.getOriginalFilename();
            imgPath = canonicalPath + "\\" + filename;
            File dest = new File(imgPath);
            String path1 = uploadPath.substring(0,uploadPath.indexOf(":")+1);
            imgPath = imgPath.substring(imgPath.lastIndexOf(path1)+2);
            map.put("dannex",imgPath);
            try{
                file.transferTo(dest);
                LOGGER.info("上传成功");
            } catch (IOException e){
                LOGGER.error(e.toString(), e);
            }
        }
        dataService.addMeans(map);
        return new ResultVO(ResultEnum.SUCCESS,"修改成功");
    }
    @RequestMapping("/getByDid")
    public Means getByDid(String did){
        Means means = dataService.getByDid(did);
        return means;
    }
    @RequestMapping("/modifyMeans")
    public ResultVO modifyMeans(@RequestPart("dannex") MultipartFile file, @RequestParam Map<String,String> map) throws IOException {
        String imgPath = "";
        if (!file.isEmpty() || file != null){
            String canonicalPath = new File(uploadPath).getCanonicalPath();
            String filename = UUID.randomUUID() + file.getOriginalFilename();
            imgPath = canonicalPath + "\\" + filename;
            File dest = new File(imgPath);
            String path1 = uploadPath.substring(0,uploadPath.indexOf(":")+1);
            imgPath = imgPath.substring(imgPath.lastIndexOf(path1)+2);
            map.put("dannex",imgPath);
            try{
                file.transferTo(dest);
                LOGGER.info("上传成功");
            } catch (IOException e){
                LOGGER.error(e.toString(), e);
            }
        }
        dataService.modifyMeans(map);
        return new ResultVO(ResultEnum.SUCCESS,"修改成功");
    }
    @RequestMapping("delByDid")
    public ResultVO delByDid(String did){
        dataService.delByDid(did);
        return new ResultVO(ResultEnum.SUCCESS,"删除成功");
    }
}
